home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / xfcn / spttool.cpt / Support Tools eXternals 1.2.5 / card_34332.txt < prev    next >
Text File  |  1990-11-13  |  12KB  |  330 lines

  1. -- card: 34332 from stack: in.5
  2. -- bmap block id: 9472
  3. -- flags: 0000
  4. -- background id: 3858
  5. -- name: FileSize
  6. ----- HyperTalk script -----
  7. on HideObjects
  8.   hide cd btn "Try It!"
  9. end HideObjects
  10.  
  11. on ShowObjects
  12.   show cd btn "Try It!"
  13. end ShowObjects
  14.  
  15.  
  16. -- part 2 (button)
  17. -- low flags: 00
  18. -- high flags: A002
  19. -- rect: left=82 top=185 right=219 bottom=175
  20. -- title width / last selected line: 0
  21. -- icon id / first selected line: 0 / 0
  22. -- text alignment: 1
  23. -- font id: 0
  24. -- text size: 12
  25. -- style flags: 8192
  26. -- line height: 16
  27. -- part name: Try it!
  28. ----- HyperTalk script -----
  29. on mouseUp
  30.   global errGlobal
  31.   put FilePath("", "Choose a file please.") into fileName
  32.   if fileName = empty then exit mouseUp
  33.   put FileSize(fileName, "nodialog:errGlobal") into fSize
  34.   if errGlobal Γëá empty then
  35.     answer "Error: ΓÇ£" & errGlobal & "ΓÇ¥"
  36.     put empty into errGlobal
  37.   else
  38.     answer "ΓÇ£" & fileName & "ΓÇ¥ takes" && fSize && "bytes of valuable" && "disk space."
  39.   end if
  40. end mouseUp
  41.  
  42.  
  43.  
  44.  
  45. -- part contents for background part 38
  46. ----- text -----
  47. 19/50
  48.  
  49. -- part contents for background part 20
  50. ----- text -----
  51. FileSize - An XFCN to return the size of a specified file
  52.  
  53. FileSize(pathname, <"noDialog:"errorGlobal>)
  54.  
  55. This XFCN will return the size of the file specified by the first parameter in BYTES not K.  It currently can not return the size of a folder, but that will be fixed soon.
  56.  
  57.  
  58. -- part contents for background part 42
  59. ----- text -----
  60. { FileSize(pathname ┬½,"nodialog":errGlobal┬╗)                      }
  61. { XFCN to return the creator for the file specified by      }
  62. { the path given in the first parameter.                        }
  63. {}
  64. {}
  65. {   Written by:      Anup Murarka         Eric Carlson                 }
  66. {               ALINK:  SKEPTIC       ALINK:  cyNic           }
  67. {                                   CIS:  76004,3356}
  68. {}
  69. {               We are part of the Support Tools Development Group,     }
  70. {               Apple Computer, Inc.      }
  71. {}
  72. {               please DO NOT contack Mac DTS for support of this code!    }
  73. {}
  74. {               please DO contact the authors for support of this code!     }
  75. {}
  76. {               Send comments, bug reports, requests to any of the above   }
  77. {               E-mail addresses or to:}
  78. {}
  79. {                           (one of us)                  }
  80. {                           Apple Computer, Inc.          }
  81. {                           900 E. Hamilton, Ave.          }
  82. {                           Campbell, CA   95008      }
  83. {                           M/S 72-L                     }
  84. {}
  85. {   Copyright:   ┬⌐ 1989, 1990 by Apple Computer, Inc., all rights reserved.     }
  86. {}
  87. { written by    : Anup Murarka                                                                               }
  88. { AppleLink  : Skeptic                                                                                      }
  89. { modification history                                                                                        }
  90. {          Date              Initials                                    Comments                                   }
  91. {          ----          ------          --------------------------------------------------     }
  92. {       8/16/89           akm         first written                                                                   }
  93. {       5/21/90           ec            removed upper case converion for A/UX compatibility.  }
  94. {                                           Changed version to 1.1                                                  }
  95. {}
  96. unit FileSize;
  97.  
  98. interface
  99.  
  100.     uses
  101.         HyperXcmd;
  102.  
  103.     procedure MAIN (paramPtr: XCmdPtr);
  104.  
  105. implementation
  106.  
  107.     procedure FileSize (paramPtr: XCmdPtr);
  108.     FORWARD;
  109.  
  110.     procedure MAIN (paramPtr: XCmdPtr);
  111.     begin
  112.         FileSize(paramPtr);
  113.     end;
  114.  
  115.     procedure reportToUser (paramPtr: XCmdPtr;
  116.                                     msgStr: str255);
  117. {}
  118. { report something back to the user.  }
  119. { the last parameter (optional) to an external may contain }
  120.  { "noDialog" or "noDialog:GlobalName".  GlobalName is the name }
  121.  { of a HyperTalk global variable into which error messages will be }
  122.  { placed.  we've decided to use this approach to avoid confusing }
  123. { an error message with a valid result being returned from an XFCN. }
  124. {}
  125.         var
  126.             tempStr: str255;
  127.     begin
  128. {check the last param to see if the user requested that}
  129. { we suppress the error dialog }
  130.         ZeroToPas(paramPtr, paramPtr^.params[paramPtr^.paramCount]^, tempStr);
  131.         UprString(tempStr, true);
  132.         if pos('NODIALOG', tempStr) = 0 then
  133.     { no special error handling specified, throw up a dialog and return the error message }
  134.             begin
  135.                 SendCardMessage(paramPtr, concat('answer "', msgStr, '"'));
  136.                 paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
  137.             end
  138.         else if (pos(':', tempStr) > 0) then
  139.     { requested global AND noDialog so we fill in the global and return empty }
  140.             begin
  141.                 tempStr := copy(tempStr, pos(':', tempStr) + 1, length(tempStr));
  142.                                                         { get the name of the HC global  to fill }
  143.                 SetGlobal(paramPtr, tempStr, PasToZero(paramPtr, msgStr));
  144.                                                         { and fill it }
  145.                 paramPtr^.returnValue := PasToZero(paramPtr, '');      { return empty }
  146.             end
  147.         else
  148.     { requested noDialog only so we return the error condition as the result }
  149.             paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
  150.     end;     { procedure }
  151.  
  152.     function AskedForHelp (paramPtr: XCmdPtr;
  153.                                     syntaxMsg: Str255;
  154.                                     copyrightMsg: Str255): boolean;
  155. {   check to see if the user sent a '?' or a '!' as }
  156. { the only parameter. if so we will respond with }
  157. { the calling syntax or the copyright/version info }
  158. { for this external }
  159. {}
  160.         var
  161.             firstStr: str255;
  162.     begin
  163.         askedForHelp := false;
  164.         if paramPtr^.paramCount = 1 then
  165.             begin
  166.                 ZeroToPas(paramPtr, paramPtr^.params[1]^, firstStr);
  167.                     { what is the first param? }
  168.                 if firstStr = '?' then
  169.                     begin
  170.                         reportToUser(paramPtr, syntaxMsg);
  171.                         askedForHelp := true
  172.                     end  { asked for help }
  173.                 else if firstStr = '!' then
  174.                     begin
  175.                         reportToUser(paramPtr, copyRightMsg);
  176.                         askedForHelp := true
  177.                     end;     { asked for copyright info }
  178.             end;     { one parameter passed }
  179.     end;     { function }
  180.  
  181.     function LongToString (paramPtr: XCmdPtr;
  182.                                     num: LONGINT): Str255;
  183. { why, oh why did dan write this one as a procedure??? }
  184.         var
  185.             tempStr: str255;
  186.     begin
  187.         LongToStr(paramPtr, num, tempStr);
  188.         LongToString := tempStr;
  189.     end;
  190.  
  191.     function NumberToString (paramPtr: XCmdPtr;
  192.                                     num: LONGINT): Str255;
  193. { use the toolbox call rather than HC's }
  194.         var
  195.             tempStr: str255;
  196.     begin
  197.         NumToString(num, tempStr);
  198.         NumberToString := tempStr;
  199.     end;
  200.  
  201.     procedure reportResError (paramPtr: XCmdPtr;
  202.                                     errorNum: integer);
  203.         var
  204.             errMsg, tempName: str255;
  205.     begin
  206.         case errorNum of                   { what caused the problem? }
  207.             -0: 
  208.                 errMsg := 'no error.';
  209.             -36: 
  210.                 errMsg := 'I/O Error.';
  211.             -37: 
  212.                 errMsg := 'bad file name or volume name.';
  213.             -38: 
  214.                 errMsg := 'file not open.';
  215.             -39: 
  216.                 errMsg := 'that file has no resource fork.';
  217.             -42: 
  218.                 errMsg := 'too many files open.';
  219.             -43: 
  220.                 errMsg := 'file not found.';
  221.             -45, -54, -61: 
  222.                 errMsg := 'file locked.';
  223.             -47, -49: 
  224.                 errMsg := 'file is busy.';
  225.             -53: 
  226.                 errMsg := 'that volume is not on line.';
  227.             -108: 
  228.                 errMsg := 'not enough room in heap zone.';
  229.             -120: 
  230.                 errMsg := 'directory not found.';
  231.             -121: 
  232.                 errMsg := 'too many working directories open.';
  233.             -127: 
  234.                 errMsg := 'internal file system error.';
  235.             -192: 
  236.                 errMsg := 'resource not found.';
  237.             -193: 
  238.                 errMsg := 'file not found.';
  239.             otherwise
  240.                 errMsg := concat('unexpected error #', NumberToString(paramPtr, errorNum));
  241.         end;         { case }
  242.  
  243.         errMsg := concat('Sorry, ', errMsg);
  244.         reportToUser(paramPtr, errMsg);
  245.         { return the error message }
  246.     end;         { function }
  247.  
  248.     function BitTest (AddressToCheck: ptr;
  249.                                     TotalBits: integer;
  250.                                     BitToTest: longint): boolean;
  251.     { function that allows caller to use std. 68000 bit notation instead of the Toolbox's reversed notation}
  252.     { example:  bit 0 (the least significant bit) in a byte is bit 7 in the Toolbox's notation}
  253.     begin
  254.         BitTest := BitTst(AddressToCheck, TotalBits - 1 - BitToTest);
  255.     end;
  256.  
  257.     function getParams (paramPtr: XCmdPtr;
  258.                                     var PathToFile: str255): boolean;
  259.     { function to get the parameters and validate them.  Returns boolean}
  260.     { instructing the main procedure to continue if the parameters passed}
  261.     { are valid.  Also returns syntax messages if requested by the user.}
  262.         var
  263.             numParams: integer;
  264.             syntaxStr, copyrightStr: str255;
  265.  
  266.     begin
  267.         getParams := true;     {Initially, assume the parameters are valid.}
  268.         syntaxStr := 'FileSize(pathname ┬½, ΓÇ£nodialogΓÇ¥:errGlobal┬╗)';
  269.         copyrightStr := '┬⌐ 1989,1990 Apple Computer, Inc., v.1.1, by Anup Murarka';
  270.  
  271.         {check that we have the proper number of parameters}
  272.         numParams := paramPtr^.paramCount;
  273.         if (numParams < 1) or (numParams > 2) then
  274.             begin
  275.                 getParams := false;
  276.                 reportToUser(paramPtr, syntaxStr);
  277.                 exit(getParams);
  278.             end;
  279.  
  280.         if AskedForHelp(paramPtr, syntaxStr, copyrightStr) then
  281.             begin
  282.                 getParams := false;
  283.                 exit(getParams);
  284.             end;
  285.  
  286.         { Get first parameter}
  287.         ZeroToPas(paramPtr, paramPtr^.Params[1]^, PathToFile);
  288.     end;     {GetParams}
  289.  
  290.     procedure FileSize (paramPtr: XCmdPtr);
  291.         var
  292.             getParamsOK: boolean;
  293.             FileName: str255;
  294.             paramBlock: CInfoPBRec;
  295.             vFileSize: longint;
  296.             errorCode: OSerr;
  297.  
  298.     begin   { filesize}
  299.     { fetch and validate the passed parameters}
  300.         getParamsOK := getParams(paramPtr, FileName);
  301.         if not (getParamsOK) then
  302.             exit(filesize);
  303.  
  304.     { Initialize the parameter block.  Since we have the full pathname,}
  305.     { no other field is really needed.}
  306.         zeroBytes(paramPtr, @paramBlock, sizeOf(paramBlock));
  307.         paramBlock.ioNamePtr := @FileName;
  308.  
  309.         errorCode := PBGetCatInfo(@paramBlock, FALSE);
  310.         if errorCode <> noErr then
  311.             begin
  312.                 reportResError(paramPtr, errorCode);
  313.                 exit(filesize);
  314.             end;
  315.  
  316.     { Is it a file or a directory?}
  317.         if BitTest(@paramBlock.ioFlAttrib, 8, 4) then
  318.             begin
  319.                 reportToUser(paramPtr, 'Sorry, I can not get the size of a directory (yet)!');
  320.                 exit(FileSize);
  321.             end;
  322.  
  323.     { Now prepare the return value.  Use FileName as a temp variable}
  324.         vFileSize := paramBlock.ioFlLgLen + paramBlock.ioFlRLgLen;
  325.  
  326.         FileName := LongToString(paramPtr, vFileSize);
  327.         paramPtr^.returnValue := PasToZero(paramPtr, FileName);
  328.     end;
  329.  
  330. end.